package com.serkol.driving.learning.api;

import cn.binarywang.wx.miniapp.api.WxMaService;
import com.serkol.bean.Result;
import com.serkol.utils.ConvertImage;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@RequestMapping("api/test")
@RestController
public class TestApiController {
     @Autowired
      private WxMaService wxMaService;
    @Autowired
    private WxMpService wxMpService;

    @RequestMapping(value = "/index")
    public Result index() throws WxErrorException {
        String accessToken = wxMaService.getAccessToken();
        System.out.println(accessToken);
        File wxaCode = wxMaService.getQrcodeService().createWxaCode("pages/index/index");
        wxaCode.setWritable(true);

        //    WxMaConfig wxMaConfig = wxMaService.getWxMaConfig();


        return Result.ok(wxaCode);
    }
    @RequestMapping(value = "/login")
    public File login() throws WxErrorException {
       WxMpQrCodeTicket hello = wxMpService.getQrcodeService().qrCodeCreateLastTicket("hello");
         String url = hello.getUrl();
        System.out.println(url);
      //  final String s = wxMpService.getQrcodeService().qrCodePictureUrl("hello");
    /*    WxMpQrCodeTicket ticket=new WxMpQrCodeTicket();
        ticket.setTicket("hello");
        ticket.setExpireSeconds(1000);*/
         File file = wxMpService.getQrcodeService().qrCodePicture(hello);
        final String s = ConvertImage.convertImageToBase64Str(file);
        System.out.println(s);
        return file;
    }
}
